package com.ssh.multithread2;

import java.util.LinkedList;
import java.util.Queue;
/**
 * 	线程池实现模型 传统模式
 * @author zuoshuaiok@gmai.com
 *
 */
public class WorkThread extends Thread {

	// public static void main(String[] args) {
	// WorkThread wt = new WorkThread();
	// wt.start();
	// for (int i = 0; i < 10; i++) {
	// wt.insertTask("--task--" + i);
	// }
	// }

	public Queue<String> queue = new LinkedList<String>();

	private boolean isStop = false;

	public WorkThread(ThreadPool pool, int i) {
		this.setName("zuo-thread-" + i);
	}

	public void insertTask(String task) {

		synchronized (queue) {
			queue.add(task);
			queue.notifyAll();
		}
	}

	@Override
	public void run() {

		while (!isStop) {

			synchronized (queue) {
				while (queue.size() == 0) {
					try {
						System.out.println("----线程" + Thread.currentThread().getName() + "进入等待任务状态");
						queue.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}

				String Task = queue.poll();
				System.out.println("----当前--" + Thread.currentThread().getName() + "--执行了任务---" + Task
						+ "-----finish----");
			}

		}

	}

}
